package com.gljx.web.controller.JournalController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.User;
import com.gljx.web.Entity.JournalEntry.TrainingStatistics;
import com.gljx.web.service.TrainingStatisticsService;

@Controller
@RequestMapping("/")
public class TrainingStatisticsController {

	@Autowired
	private TrainingStatisticsService trainingStatisticsService;
	
	/**
	 * 查询当月的考生，培训，考试人数统计报表
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTrainingStatistics/look.action")
	public String queryTrainingStatistics(HttpServletRequest request){
		//获取当前月的第一天，和最后一天
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		request.setAttribute("beginTime", TimeFormat.getFirstdayOfMonth(new Date()));
		request.setAttribute("endTime", TimeFormat.getLastdayOfMonth(new Date()));
		request.setAttribute("time", sdf.format(new Date()));
 		return "/backHome/journaling/financialJournaling/trainingStatistics/dayMain";
	}
	
	/**
	 * 遍历一个月的时间
	 * @param time
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTrainingStatisticsByTime")
	@ResponseBody
	public List<String> queryTrainingStatisticsByTime(String startTime) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		List<String> time = new ArrayList<>();
		String beginTime = TimeFormat.getFirstdayOfMonth(sdf.parse(startTime));
		String endTime = TimeFormat.getLastdayOfMonth(sdf.parse(startTime));
		Integer start = Integer.valueOf(beginTime.substring(beginTime.length()-2));
		Integer end = Integer.valueOf(endTime.substring(endTime.length()-2));
		for (int i = start; i <= end; i++) {
			if(i < 10){
				time.add(String.valueOf("0"+i));
			}else{
				time.add(String.valueOf(i));
			}
		}
		return time;
	}
	
	/**
	 * 查询数据
	 * @param request
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryTrainingStatisticsData")
	@ResponseBody
	public Map<String, List<TrainingStatistics>> queryTrainingStatisticsData(HttpServletRequest request,String startTime,String endTime) throws Exception{
		Map<String, List<TrainingStatistics>> map = new HashMap<>();
		User user = (User)request.getSession().getAttribute("user");
		//查询所有招生人数
		List<TrainingStatistics> trainingStatistics = trainingStatisticsService.queryStudentByTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("trainingStatistics", trainingStatistics);
		//查询所有的科目二培训人数
		List<TrainingStatistics> kmepxrs = trainingStatisticsService.queryShareCarTwoRegByCar(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmepxrs", kmepxrs);
		//查询所有的科目二及格人数
		List<TrainingStatistics> kmejgrs = trainingStatisticsService.querySubjectTwoResultEntryByTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmejgrs", kmejgrs);
		//查询所有的科目三培训人数
		List<TrainingStatistics> kmspxrs = trainingStatisticsService.queryShareCarThreeRegEntryByTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmspxrs", kmspxrs);
		//查询所有的科目三及格人数
		List<TrainingStatistics> kmsjgrs = trainingStatisticsService.querySubjectThreeResulEntryByTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmsjgrs", kmsjgrs);
		//查询所有的非本校科目二考试人数
		List<TrainingStatistics> kmewxksrs = trainingStatisticsService.querySubjectTwoResultEntryByYiDiAndTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmewxksrs", kmewxksrs);
		//查询所有的非本校科目三考试人数
		List<TrainingStatistics> kmswxksrs = trainingStatisticsService.querySubjectThreeResultEntryByYiDiAndTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmswxksrs", kmswxksrs);
		//查询所有的科目二考前训练人数
		List<TrainingStatistics> kmekqxlrs = trainingStatisticsService.queryOwnCarTrainEntryByTime(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmekqxlrs", kmekqxlrs);
		//查询所有的科目三考前训练人数
		List<TrainingStatistics> kmskqxlrs = trainingStatisticsService.queryOwnCarTrainEntryByTimeAndkms(user.getFcompanyid(), startTime+" 00:00:00", endTime+ "23:59:59");
		map.put("kmskqxlrs", kmskqxlrs);
		return map;
	}
	
	/**
	 * 获取当前
	 * @return
	 */
	@RequestMapping("/queryTrainingStatisticsMonth/look.action")
	public String queryTrainingStatisticsMonth(HttpServletRequest request){
		//时间格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		request.setAttribute("time", sdf.format(new Date()));
		return "/backHome/journaling/financialJournaling/trainingStatistics/monthMain";
	}
	
	/**
	 * 根据年份查询数据
	 * @param request
	 * @param startTime 开始月
	 * @param endTime 结束月
	 * @return
	 */
	@RequestMapping("/queryTrainingStatisticsDataByMonth")
	@ResponseBody
	public Map<String, List<TrainingStatistics>> queryTrainingStatisticsDataByMonth(HttpServletRequest request,String startTime,String endTime){
		//由于查询不到最后一个月，所以最后一个月加上一个月
		endTime = TimeFormat.sumOneMonth(endTime);
		Map<String, List<TrainingStatistics>> map = new HashMap<>();
		User user = (User)request.getSession().getAttribute("user");
		//查询所有招生人数
		List<TrainingStatistics> trainingStatistics = trainingStatisticsService.queryStudentByMonth(user.getFcompanyid(), startTime, endTime);
		map.put("trainingStatistics", trainingStatistics);
		//查询所有的科目二培训人数
		List<TrainingStatistics> kmepxrs = trainingStatisticsService.queryShareCarTwoRegByCarAndMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmepxrs", kmepxrs);
		//查询所有的科目二及格人数
		List<TrainingStatistics> kmejgrs = trainingStatisticsService.querySubjectTwoResultEntryByMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmejgrs", kmejgrs);
		//查询所有的科目三培训人数
		List<TrainingStatistics> kmspxrs = trainingStatisticsService.queryShareCarThreeRegEntryByMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmspxrs", kmspxrs);
		//查询所有的科目三及格人数
		List<TrainingStatistics> kmsjgrs = trainingStatisticsService.querySubjectThreeResulEntryByMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmsjgrs", kmsjgrs);
		//查询所有的非本校科目二考试人数
		List<TrainingStatistics> kmewxksrs = trainingStatisticsService.querySubjectTwoResultEntryByYiDiAndMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmewxksrs", kmewxksrs);
		//查询所有的非本校科目三考试人数
		List<TrainingStatistics> kmswxksrs = trainingStatisticsService.querySubjectThreeResultEntryByYiDiAndMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmswxksrs", kmswxksrs);
		//查询所有的科目二考前训练人数
		List<TrainingStatistics> kmekqxlrs = trainingStatisticsService.queryOwnCarTrainEntryByMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmekqxlrs", kmekqxlrs);
		//查询所有的科目三考前训练人数
		List<TrainingStatistics> kmskqxlrs = trainingStatisticsService.queryOwnCarTrainEntryByTimeAndkmsAndMonth(user.getFcompanyid(), startTime, endTime);
		map.put("kmskqxlrs", kmskqxlrs);
		return map;
	}
}
